package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("删除图书....");
        Scanner scanner = new Scanner(System.in);
        System.out.printf("请输入你要删除的图书：");
        String name = scanner.nextLine();
        int size = bookList.getBooksize();
        //查找
        int pos = 0;
        int i = 0;
        for (; i < size; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                pos = i;
                break;
            }
        }
        if(size == i){
            System.out.println("没有这本图书！");
            return;
        }
        //删除
        //bookList[j] = bookList[j+1]
        for (int j = pos; j < size-1; j++) {
            Book book = bookList.getBook(j+1);
            bookList.setBook(j,book);
        }
        //有效个数-1
        bookList.setBooksize(size-1);
        System.out.println("删除成功！！");
    }
}
